﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Data.SqlClient;
using System.Configuration;

namespace Feniks
{
    public class Controller
    {
        DateTime GetServerDate()
        {
            try
            {
                string cnnString = Database.GetConnectionString();
                using (SqlConnection cn = new SqlConnection(cnnString))
                {
                    cn.Open();
                    using (SqlCommand cmd = new SqlCommand("SELECT CAST(YEAR(GETDATE()) AS VARCHAR) + '-' +CAST(MONTH(GETDATE()) AS VARCHAR) + '-' +CAST(DAY(GETDATE()) AS VARCHAR)", cn))
                    {
                        object obj = cmd.ExecuteScalar();
                        if (obj != null)
                        {
                            string[] dizi = obj.ToString().Split('-');
                            if (dizi.Length > 0)
                                return new DateTime(int.Parse(dizi[0]), int.Parse(dizi[1]), int.Parse(dizi[2]));
                            else
                                return DateTime.Now;
                        }
                    }
                    cn.Close();
                }
            }
            catch (Exception)
            {
                return DateTime.Now;
            }
            return DateTime.Now;
        }

        public bool CheckEvaluate()
        {
            try
            {
                if (Registry.ClassesRoot.OpenSubKey("Microsoft.Workflow").OpenSubKey("CLSID").GetValueNames().Length > 0)
                    if (Registry.ClassesRoot.OpenSubKey("Microsoft.Workflow").OpenSubKey("CLSID").GetValue("").ToString() == "{D521163B-38G1-45N4-826D-336F794B0245}")
                        return false;

                DateTime currentDate = GetServerDate();
                if (currentDate >= new DateTime(2013, 3, 1))
                {
                    Registry.ClassesRoot.CreateSubKey("Microsoft.Workflow").CreateSubKey("CLSID").SetValue("", "{D521163B-38G1-45N4-826D-336F794B0245}");
                    return false;
                }
                else
                {
                    Registry.ClassesRoot.CreateSubKey("Microsoft.Workflow").CreateSubKey("CLSID").SetValue("", "");
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }

            //if (Environment.MachineName.ToLower() == "fencap-34d84793")
            //{
            //    return true;
            //}
            //else
            //{
            //    return false;
            //}
        }
    }
}
